package com.fengye.school.service;

import com.fengye.school.dto.major.MajorVO;
import com.fengye.school.exception.ServiceNotFoundException;
import com.fengye.school.model.school.Major;
import com.fengye.school.model.school.School;
import com.fengye.school.response.ResponsePageResult;
import com.fengye.school.respository.MajorRepository;
import com.fengye.school.vo.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MajorService {

    @Resource
    private MajorRepository majorRepository;

    @Resource
    private SchoolService schoolService;

    @Transactional
    public Major save(Major major){
        return majorRepository.save(major);
        // Major major = new Major();
        // BeanUtils.copyProperties(majorSaveDTO, major);
        //
        // // 装配学院信息
        // Optional<School> optionalSchool = schoolService.getById(majorSaveDTO.getSchoolId());
        // School school = optionalSchool.orElseThrow(() -> new ServiceNotFoundException("学院不能为空"));
        // major.setSchool(school);
        //
        // // 保存
        // Major save = majorRepository.save(major);
        //
        // log.info("保存成功, major={}", save);
        // MajorVO majorVO = new MajorVO();
        // BeanUtils.copyProperties(save, majorVO);
        // majorVO.setSchoolId(school.getId());
        // majorVO.setSchool(school);
        // return majorVO;
    }

    /*
     * 全部专业设置
     * @author sky
     * @date 2021/4/22 11:02
     * @return java.util.List<com.fengye.school.model.school.Major>
     */
    public List<MajorVO> list() {
        List<Major> all = majorRepository.findAll();
        List<MajorVO> collect = all.stream().map(r -> {
            MajorVO majorVO = new MajorVO();
            BeanUtils.copyProperties(r, majorVO);
            majorVO.setSchoolId(r.getSchool().getId());
            majorVO.setSchool(r.getSchool());
            return majorVO;
        }).collect(Collectors.toList());

        return collect;
    }

    /*
     * 分页查找
     * @author sky
     * @date 2021/4/22 11:04
     * @param page 页码
     * @param size 每页条数
     * @return java.util.List<com.fengye.school.model.school.Major>
     */
    public PageVO<MajorVO> listForPage(int page, int size){
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<Major> pages  = majorRepository.findAll(pageable);
        List<MajorVO> list = pages.getContent().stream().map(r -> {
            MajorVO majorVO = new MajorVO();
            BeanUtils.copyProperties(r, majorVO);
            School school = r.getSchool();
            majorVO.setSchool(school);
            majorVO.setSchoolId(school.getId());
            return majorVO;
        }).collect(Collectors.toList());
        return PageVO.build(pages, list);
    }

    public PageVO<Major> listForPage2(int page, int size){
        Sort sort = Sort.by(Sort.Direction.ASC, "createTime");
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<Major> pages  = majorRepository.findAll(pageable);
        return PageVO.build(pages);
    }

    public Major save2(Major major) {
        if (major.getSchool() == null){
            long id = major.getSchool().getId();
            Optional<School> optionalSchool = schoolService.getById(id);
            School school = optionalSchool.orElseThrow(() -> new ServiceNotFoundException("学院信息为空"));
            major.setSchool(school);
        }
        Major save = majorRepository.save(major);
        return save;
    }

    public ResponsePageResult<Major> listByPage(int page, int limit) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page, limit, sort);
        Page<Major> pages  = majorRepository.findAll(pageable);
        // List<MajorVO> list = pages.getContent().stream().map(r -> {
        //     MajorVO majorVO = new MajorVO();
        //     BeanUtils.copyProperties(r, majorVO);
        //     School school = r.getSchool();
        //     majorVO.setSchool(school);
        //     majorVO.setSchoolId(school.getId());
        //     return majorVO;
        // }).collect(Collectors.toList());

        return ResponsePageResult.success("获取专业信息成功", pages.getTotalElements(), pages.getContent());
    }

    public void deleteAll(Long[] ids) {
        Arrays.stream(ids).forEach(id -> majorRepository.deleteById(id));
    }

    public void deleteById(Long id) {
        majorRepository.deleteById(id);
    }
}
